<?php
class QuestionsController extends AppController 
{

    public $sN = 'Question'; 
    public $pN = 'question';
    public $lN = 'questions';    

    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel("Survey");
        $this->loadModel("SurveyQuestion");
        $this->set('sN', $this->sN);        
        $this->set('pN', $this->pN);
        $this->set('lN', $this->lN);      
    }

    public function admin_index($survey_id=null) {
        $survey = $this->Survey->read(null,$survey_id);
        if($survey['Survey']['type']==Survey::EXAMINATION){
            $values = $this->paginate($this->sN,array('has_answer'=>1));
        }else{
            $values = $this->paginate($this->sN);
        }
        $this->set("survey_id",$survey_id);
      	$this->set('values', $values);
        $this->set("survey",  $survey);
    }
    public function admin_add_to_survey($question_id,$survey_id){
        $this->SurveyQuestion->create();
        $this->SurveyQuestion->save(array('surveys_id'=>$survey_id,'questions_id'=>$question_id));
        $this->Session->setFlash("The Question has been added to survey");
        $this->redirect(array('controller' => 'questions', 'action' => 'index',$survey_id, 'admin' => true));
    }
    public function admin_remove_from_survey($question_id,$survey_id){
        $this->SurveyQuestion->delete_survey_question($survey_id,$question_id);
        $this->Session->setFlash("The Question has been removed to survey");
        $this->redirect(array('controller' => 'surveys', 'action' => 'questions',$survey_id, 'admin' => true));
    }
    public function admin_add($survey_id=null){
        $this->loadModel("Question");
        if(!empty($this->data)) {
            $submit = $this->request->data["Question"]['submit'];
            unset($this->request->data["Question"]['submit']);
            if(count($this->request->data["Question"]['answer'])>1){
                $this->request->data["Question"]['type']="multiselect";
            }
            $this->request->data["Question"]['created_by'] = $this->Auth->user('id');
            $this->request->data["Question"]['answer']=  implode(",", $this->request->data["Question"]['answer']);
            if($this->Question->save($this->data)) {
                $question_id = $this->Question->id;
                if($survey_id){
                    $this->SurveyQuestion->create();
                    $this->SurveyQuestion->save(array('surveys_id'=>$survey_id,'questions_id'=>$question_id));
                    if($submit=="Save"){
                        $this->Session->setFlash("Update Suqqccessfully!");
                        $this->redirect(array('controller' => 'surveys', 'action' => 'questions',$survey_id, 'admin' => true));
                    }else{
                        $this->redirect(array('controller' => 'questions', 'action' => 'add',$survey_id, 'admin' => true));
                    }
                }else{
                    if($submit=="Save"){
                        $this->Session->setFlash("Update Successfully!");
                        $this->redirect(array('controller' => 'questions', 'action' => 'index', 'admin' => true));
                    }else{
                        $this->redirect(array('controller' => 'questions', 'action' => 'add', 'admin' => true));
                    }
                }
            }
        }
        $this->set("survey",  $this->Survey->read(null,$survey_id));
        $this->set("survey_id",$survey_id);
    }
    public function admin_get($id=null){
        $this->autoLayout = false;
        $question = $this->Question->read(null,$id);
        $this->render("/Elements/question");
    }
    public function admin_edit($id=null){
        if(empty($this->data)) {
            $this->request->data = $this->Question->read(null, $id);
            $ans = explode(",", $this->request->data['Question']['answer']);
            $arr = array();
            foreach ($ans as $value) {
                //$val = str_replace("option_", "", $value);
                $arr[]=$value;
            }
            $this->set("selected_groups",$arr);
            //$this->request->data['Question']['answer'] = $arr;
        }else{
            $this->Question->id = $id;
            if(count($this->request->data["Question"]['answer'])>1){
                $this->request->data["Question"]['type']="multiselect";
            }
            $this->request->data["Question"]['answer']=  implode(",", $this->request->data["Question"]['answer']);
            if($this->Question->save($this->data)){
                $this->Session->setFlash(__('Updated Successfully', true));
                $this->redirect(array('controller' => 'questions', 'action' => 'index', 'admin' => true));
            }
        }
    }
    public function admin_delete($id = null) {
        if(!$id){
            $this->Session->setFlash(__('Invalid Question id', true));
            $this->redirect(array('controller' => 'shopper_groups', 'action' => 'index', 'admin' => true));
        }else{
            $this->Question->id = $id;
            if($this->Question->delete()) {
                $this->SurveyQuestion->delele_survey_by_question($id);
                $this->Session->setFlash(__('Question has been deleted', true));
                $this->redirect(array('controller' => 'questions', 'action' => 'index', 'admin' => true));
            }
        }
    }
}